<?php

class student extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper('url');
        $this->load->model('student_model');
        if($this->session->userdata('user_id')== null){
            redirect(base_url());
        }
    } 
     
    /* Student Display methods */

    public function display() {
        $data['student'] = $this->student_model->getStudent($this->session->userdata('user_id'));
        if ($data['student']['level_number'] == 0) {
            $data['title'] = 'First Login';
            $this->load->view('templates/header', $data);
            $this->load->view('firstlogin', $data);
            $this->load->view('templates/footer');
        } else {
            $this->dashboard();
        }
    }

    public function edit() {
        $userid = $this->session->userdata('user_id');
        $data['profile'] = $this->student_model->getStudent($userid);
        $data['profile_link'] = base_url('/student/dashboard/profile');
        $data['title'] = 'Edit Student';
        $data['cancel_link'] = base_url('/student/dashboard/');

        $this->load->helper('form', 'url');
        $this->load->library('form_validation');

        $this->form_validation->set_rules('firstname', 'First Name', 'trim|required');
        $this->form_validation->set_rules('middlename', 'Middle Name', 'trim');
        $this->form_validation->set_rules('lastname', 'Last Name', 'trim|required');

        if ($this->form_validation->run() === FALSE) {
            $this->load->view('templates/header', $data);
            $this->load->view('EditProfile', $data);
            $this->load->view('templates/footer');
        } else {
            $firstname = ucwords(strtolower($this->input->post('firstname')));
             $middlename = ucwords(strtolower($this->input->post('middlename')));
             $lastname = ucwords(strtolower($this->input->post('lastname')));
            $studentdata = array('firstname' => $firstname, 'middlename' => $middlename, 'lastname' => $lastname);
            $this->student_model->updateStudent($userid, $studentdata);
            redirect(base_url('/student/dashboard/profileupdate'));
        }
    }

    public function dashboard($tab = '') {
        $this->evaluate();
        $userid = $this->session->userdata('user_id');
        $data['profile'] = $this->student_model->getStudent($userid);
        $data['color'] = $this->student_model->getColor($data['profile']['level_number']);
        $data['criterias'] = $this->student_model->getAllCriterias($userid);
        $data['scores'] = $this->student_model->getScores($userid);
        $data['chart'] = $this->student_model->forChart($userid,$data['profile']['level_number']);
        $data['title'] = 'Dashboard';
        if (!empty($tab)) {
            $data['activeMenu'] = 'myProfile';
            if ($tab == 'changepass') {
                $data['alert'] = 'Successfully updated password.';
            }else if ($tab == 'errpass') {
                $data['error'] = 'Old password entered is invalid.';
            }else if($tab == 'profileupdate'){
                 $data['alert'] = 'Successfully updated profile.';
            }
        }
        $this->load->view('templates/header', $data);
        $this->load->view('student');
        $this->load->view('templates/footer');
    }

    /* End for Student Display methods */

    public function evaluate() {
        $id = $this->session->userdata('user_id');
        $criteria = $this->student_model->getCriteria('B', $id);

        if (empty($criteria)) {
            $criteria = $this->student_model->getCriteria('C', $id);
            if (empty($criteria)) {
                $location = base_url('progress/onLoadUpdate');
                redirect($location);
            }
        }
        $_SESSION['criteria'] = $criteria;
        return;
    }

    public function readStory() {
        $this->evaluate();
        $location = base_url('story/display/');
        redirect($location);
    }

    public function studentReport($userid) {
        $data['profile'] = $this->student_model->getStudent($userid);
        $data['color'] = $this->student_model->getColor($data['profile']['level_number']);
        $data['criterias'] = $this->student_model->getAllCriterias($userid);
        $data['scores'] = $this->student_model->getScores($userid);
        $data['chart'] = $this->student_model->forChart($userid,$data['profile']['level_number']);
        $this->load->view('studentProgress', $data);
    }

    public function changePassword($userid) {
        $student_profile = $this->student_model->getStudent($userid);
        $password = $student_profile['password'];
        $oldpass = $this->input->post('currentPass');
        $newpass = $this->input->post('newPass');
        if ($password == $oldpass) {
            $userdata = array('password' =>  $newpass);
            $this->student_model->updateUser($userid, $userdata);
            redirect(base_url('/student/dashboard/changepass'));
        } else {
            redirect(base_url('/student/dashboard/errpass'));
        }
    }

}

?>
